<?php
class UserController 
	extends Zend_Controller_Action 
{

	public function indexAction()
	{
		
		$userTypes = Vs_User::$types;
		$userSexs = Vs_User::$sexs;
		
		$mapper = Vs_Factory::mapper( 'City' );
		$cities = $mapper->getByParams();
		 
		$form = new Adm_Form_User(array(
			'passwordRequiered' => false,
			'userRequiered'		=> false,
			'exclude'			=> $this->user->email,
			'isFacebookUser'	=> $this->user->isFacebookUser(),
			'userTypes' => $userTypes,
			'userSexs' => $userSexs,
			'cities' => $cities,
		));

		$form->setAction(		
			$this->getHelper('Url')->url(array())
		);
		
		if($this->getRequest()->isPost()){
			
			$data = $this->getRequest()->getPost();

			if ( $form->isValid($data)) {
								
				$this->user->name		= $data['name'];
				$this->user->surname	= $data['surname'];
				$this->user->birthdate	= $data['birthdate'];
				$this->user->sex		= $data['sex'];
				$this->user->cityId		= $data['cityId'];
				
				if ( ! $this->user->isFacebookUser() && !empty($data['password']) ){
					$this->user->setPassword( $data['password'] );
				}
				
				$mapper = Vs_Factory::mapper( 'User' );
				$mapper->save( $this->user );
				
				$this->getHelper('MyFlashMessenger')->addSuccess('Los datos fueron actualizados');
				return $this->getHelper('Redirector')->gotoRoute(array());				
			} else {
			
				$form->populate($data);
			
			}	
		} else {
			$form->populate(array(
				'name'		=> $this->user->name,
				'surname'	=> $this->user->surname,
				'birthdate'	=> $this->user->birthdate,
				'sex'		=> $this->user->sex,
				'cityId'	=> $this->user->cityId
			));
		}	
		
		$this->view->form = $form;
		$this->render('index');
	}
	
	public function registerAction() {
		
		$config = Zend_Registry::get('configuration') ;
		
		$form = new Web_Form_Cliente(array(
			'pubkey' => $config->recaptcha->pubkey,
			'privkey'=> $config->recaptcha->privkey
		));
		$form->setAction(		
			$this->getHelper('Url')->url()
		);		
				
		
		if($this->getRequest()->isPost()){
			
			$data = $this->getRequest()->getPost();

			if ( $form->isValid($data)) {

				$user = new Vs_User ($data);
				$mapper = Vs_Factory::mapper( 'User' );
				$mailer = new Vs_Mailer;
				$mapper->save( $user );
				
				//Mail	
				$mail = new My_Mail();
				
				$mail->setTo( $user->email );
				$mail->name = $user->name;
				$mail->lang = $this->lang;
				$mail->headerType = 'default';
				
				$params = array(
					'email' => $user->email, 
					'code'  => $user->code,
					'controller'  => 'user',
					'action'  => 'activate'
				);
				
				$link =
				
				$mail->link = $this->view->serverUrl() 
							. $this->getHelper('url')->url( $params);
							
				$mail->setSubject( "Registración en {$this->system}");
				$mail->setScriptHtml("confirm-user.phtml", 'layout-html');
				
				$mailer->queue( $mail, 2 );
				
				
				$this->getHelper('MyFlashMessenger')->addSuccess($this->_helper->Translate('registrationCompletedSuccessfully'));
				return $this->getHelper('Redirector')->gotoRoute(array());				
			} else {
			
				$form->populate($data);
			
			}	
		}	
		
		if($this->_request->isXmlHttpRequest()){
			 $this->_helper->layout->disableLayout();
		}
		
		$this->view->form = $form;
		
	}
	
	public function activateAction(){
	
		$email 	= $this->getRequest()->getParam('email');
		$code	= $this->getRequest()->getParam('code');

		$mapper = Vs_Factory::mapper( 'User' );
		$user = $mapper->getByEmail($email);
		$user->activate($code);
		
		$mapper->save( $user );
		
		$this->getHelper('MyFlashMessenger')->addSuccess($this->_helper->Translate('registrationCofirmed'));
		//pase lo que pase lo mando a la pagina del login
		return $this->getHelper('Redirector')->gotoRoute(array('controller' => 'authentication', 'action' => 'login'));		
	
	}
	
	public function forgotPasswordAction(){
	
		$form = new Web_Form_Email;
		$form->setAction(	
			$this->getHelper('Url')->url(array(array('action' => 'forgot-password')))
		);
		

		try{			
			$mapper	= Vs::mapper( 'User' );
			$mailer = new Vs_Mailer;
			$user = $mapper->getByEmail ($this->getRequest()->getParam('email'));
			// regenero un codigo para usar en un chequeo
			$user->forgotPassword();					
			$mapper->save( $user );
			
			//Mail	
			$mail = new My_Mail();
			
			$mail->setTo( $user->email );
			$mail->lang = $this->lang;
		    $mail->headerType = 'default';
			$mail->name = $user->name;
			$mail->link = $this->view->serverUrl()
						. $this->_helper->url('change-password', 'user', null, array('email'=>$user->email, 'code'=>$user->code));
			
			$mail->setSubject( sprintf( $this->_helper->Translate('newPassword'), $this->system));
			$mail->setScriptHtml('new-password.phtml', 'layout-html');			
			
			$mailer->queue( $mail );
			
			$result = array(
				'success' => true,
				'message'=>	$this->_helper->Translate('checkYourEmail')		
			);			
			$this->_helper->Json($result);
				
		} catch (Vs_Exception $e){				
			$this->getHelper('Log')->exception( $e );
			
			
			$result = array(
				'success' => false,
				'message'=>	sprintf( $this->_helper->Translate('userNotInDb'),$this->getRequest()->getParam('email'))	
			);			
			$this->_helper->Json($result);
			
		}catch (Zend_Exception $e){
			$this->getHelper('Log')->exception( $e );
			
			$result = array(
				'success' => false,
				'message'=>	$this->_helper->Translate("applicationError")
			);			
			$this->_helper->Json($result);
			
		} 
	
		$this->view->form = $form;
		$this->render('forgot-password');
	} 
	
	public function changePasswordAction()
	{
		$email 	= $this->getRequest()->getParam('email');
		$code	= $this->getRequest()->getParam('code');	
		
		$form = new Web_Form_Password;
		$form->setAction(		
			$this->getHelper('Url')->url(array('email'=>$email, 'code'=>$code))
		);		
		
		try{
			
			$mapper	= Vs_Factory::mapper( 'User' );		
			$user = $mapper->getByEmail ($user);
		
			if ( $this->getRequest()->isPost() ){
				
				$data = $this->getRequest()->getPost();
				if ($form->isValid($data)){
						
					$user->changePassword( $this->getRequest()->getParam('password'), $code );
					$mapper->save( $user );
					$this->getHelper('MyFlashMessenger')->addSuccess( $this->_helper->Translate('passwordHasBeenChanged') );				
					return $this->getHelper('Redirector')->gotoRoute(array('controller'=>'authentication', 'action' => 'login'));	
					
				} else {
					$form->populate($data);				
				}

			} 
		
		} catch ( Vs_Exception $e ){
			$this->getHelper('Log')->exception( $e );
			$this->getHelper('MyFlashMessenger')->addError( $e->getMessage() );
			throw $e;
		} 
		
		$this->view->form = $form;
		$this->render('change-password');
	}
	
}